package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdGroupCriterion;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdGroupCriterionStatus;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class GxAdGroupCriterion extends AdGroupCriterion {
    private long gxId;

    public GxAdGroupCriterion(Element element) throws IllegalArgumentException, GxItemException {
        if (element.getElementsByTagName("AdGroupId").getLength() > 0 &&
            element.getElementsByTagName("AdGroupId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("AdGroupId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setAdGroupId(Long.valueOf(((Node) element.getElementsByTagName("AdGroupId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Criterion").getLength() > 0 ) {
            this.setCriterion(new GxCriterion((Element) element.getElementsByTagName("Criterion").item(0)));
        }
        if (element.getElementsByTagName("GxAdGroupCriterionId").getLength() > 0 &&
            element.getElementsByTagName("GxAdGroupCriterionId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxAdGroupCriterionId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxAdGroupCriterionId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external AdGroupCriterion id");
        }
        if (element.getElementsByTagName("BingAdGroupCriterionId").getLength() > 0 &&
            element.getElementsByTagName("BingAdGroupCriterionId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingAdGroupCriterionId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingAdGroupCriterionId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Status").getLength() > 0 &&
            element.getElementsByTagName("Status").item(0).getChildNodes().getLength() > 0) {
            if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("A")) {
                this.setStatus(AdGroupCriterionStatus.Active);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("D")) {
                this.setStatus(AdGroupCriterionStatus.Deleted);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("P")) {
                this.setStatus(AdGroupCriterionStatus.Paused);
            }
        }
        if (element.getElementsByTagName("Type").getLength() > 0 &&
            element.getElementsByTagName("Type").item(0).getChildNodes().getLength() > 0) {
            this.setType(((Node) element.getElementsByTagName("Type").item(0).getChildNodes().item(0)).getNodeValue());
        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }
}
